package com.sencha.myblog;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(HttpClient
				.doGet("http://blog.sina.com.cn/rss/1190363061.xml"));

	}

	public static String doGet(String url) {
		StringBuffer result = new StringBuffer();
		try {
			URL myURL = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
			conn.setRequestMethod("GET");

			InputStreamReader insr = new InputStreamReader(
					conn.getInputStream());

			int respInt = insr.read();
			while (respInt != -1) {
				result.append((char) respInt);
				respInt = insr.read();
			}

			insr.close();
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	public static String doPost(String url, String content) {
		StringBuffer result = new StringBuffer();
		try {
			URL myURL = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");

			OutputStream output = conn.getOutputStream();
			output.write(content.getBytes());
			output.flush();
			output.close();

			InputStreamReader insr = new InputStreamReader(
					conn.getInputStream());

			int respInt = insr.read();
			while (respInt != -1) {
				result.append((char) respInt);
				respInt = insr.read();
			}

			insr.close();
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}
}
